package com.dd.lnetwork

import com.dd.lnetwork.provider.IProvider
import com.dd.lnetwork.provider.ProviderManger
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.lang.reflect.ParameterizedType
import kotlin.properties.Delegates

open class RetrofitHelperAbs<T> constructor(var deviceId: String,
                                            var token: String?,
                                            var cookies: HashSet<String>) {
    private var api: T? = null

    init {
        create()
    }

    private fun create() {
        val client: OkHttpClient = ProviderManger.getInstance(deviceId, token, cookies)
            .build(IProvider::class.java)
            .getHttpClient(HttpLoggingInterceptor.Level.BODY)

        this.api = ProviderManger.getInstance(deviceId, token, cookies)
            .build(IProvider::class.java)
            .create(getTClass(), client)
    }

    private fun getTClass(): Class<T> {
        return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<T>
    }

    fun getDzjApi(): T {
        return api!!
    }
}